Page({
    data: {
        url: "https://h5.ihhbuy.com/h5.html?t=" + new Date().getTime(),
        url1: "",
    },
    onLoad: function (options) {
        //console.log(options); // 页面跳转时传递的参数
        this.data.url1 = this.data.url+"#/?id=" + options.id;
        console.log(this.data.url1);

        //console.log(this.data.url1);
        this.setData({
            url1: this.data.url1, // 或者根据 options 设置
        });
    },
    onShareAppMessage(options) {
        // my.alert({content: JSON.stringify(options.webViewUrl)});
        // return {
        //     title: '分享 web-View 组件',
        //     desc: 'View 组件很通用',
        //     path: 'page/component/component-pages/webview/baidu',
        //     'web-view': options.webViewUrl,
        // };
    },
    requestSubscribeMessage(value) {

    },
    customBuriedEvents({value, activeElement}) {
        const {canvasComponentId, componentName, diyId} = value;
        const {title, propsValue} = activeElement;
        let key = `${diyId}_${componentName}_${canvasComponentId}`;
        if (title) {
            key += `_${title}`
        }
        const eventParams = {
            key
        }
        if (propsValue.input_product_id) {
            eventParams.productId = propsValue.input_product_id.value
        }
        //console.log(eventParams);
    },
    handleCustomHeader(pageStyleInfo) {
        //console.log(pageStyleInfo)
        my.setNavigationBar({
            title: pageStyleInfo.name,
            frontColor: pageStyleInfo.nameColor,
            backgroundColor: pageStyleInfo.nameBgColor,
        })
    },
    onmessage(e) {


        const {type, value, activeElement, pageStyleInfo} = e.detail;

        // console.log(e);
        // console.log(type);
        // console.log(value);
        // console.log(activeElement);
        // console.log(pageStyleInfo);

        switch (type) {
            case "jump_page":
                my.navigateToMiniProgram({
                    path: value
                });
                break;
            case "jump_alipays":

                // 创建一个 URL 对象
                if (value.includes('appId')) {
                    var queryString = value.replace('alipays://platformapi/startapp?', '');
                    //console.log(queryString)
                    const keyValuePairs = queryString.split('&');
                    const result = {};
                    keyValuePairs.forEach(pair => {
                        const [key, value] = pair.split('=');

                        if (key === 'page') {
                            // 解析 page 参数
                            const decodedValue = decodeURIComponent(value);
                            const [path, queryParamsString] = decodedValue.split('?');

                            // 设置路径
                            result.path = path;

                            // 解析查询参数
                            if (queryParamsString) {
                                const queryParams = queryParamsString.split('&');
                                result.query = {};
                                queryParams.forEach(queryParam => {
                                    const [queryKey, queryValue] = queryParam.split('=');
                                    result.query[queryKey] = queryValue;
                                });
                            }
                        } else {
                            // 设置其他键值对
                            if(key ){
                                result[key] = decodeURIComponent(value);
                            }
                        }
                    });

                    my.navigateToMiniProgram(result);
                    break;
                    //console.log(result);
                    // const params = new URLSearchParams(queryString);
                    // const appId = params.get('appId');
                    // const page = params.get('page');
                    // console.log(appId);
                    // console.log(page);
                }


                // const result = {
                //     appId: appId,
                //     path: path,
                //     query: {}
                // };
                // console.log(result)
                //my.navigateToMiniProgram(result);


                // my.ap.openURL({
                //     url: value,
                // });
                break;
            case "subscribe_message":
                this.requestSubscribeMessage(value);
                break;
            case "burial_point":
                this.customBuriedEvents({value, activeElement});
                break;
            case "custom_header":
                this.handleCustomHeader(pageStyleInfo);
                break;
        }
        // my.alert({
        //     content: '拿到数据' + JSON.stringify(e), // alert 框的标题
        // });
    }
})